package com.itheima.edu.manager.controller;

import com.itheima.edu.manager.domain.Student;
import com.itheima.edu.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    //开启学生管理系统，并展示学生管理系统菜单
    public void start() {

        studentLoop:
        while (true) {

            Scanner scanner = new Scanner(System.in);
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");

            String choice = scanner.next();
            switch (choice) {
                case "1":
                    addStudent();
                    //System.out.println("添加学生");
                    break;
                case "2":
                    deleteStudentById();
                    // System.out.println("删除学生");
                    break;
                case "3":
                    updateStudent();
                    //System.out.println("修改学生");
                    break;
                case "4":
                    findAllStudent();
                    //System.out.println("查看学生");
                    break;
                case "5":
                    System.out.println("退出");
                    break studentLoop;
                default:
                    System.out.println("输入错误，调整后重新输入！");
            }
        }
    }

    private void updateStudent() {
        StudentService studentService=new StudentService();
            Scanner scanner=new Scanner(System.in);
        String updateID;
        while (true){
            System.out.println("请输入要修改的学生ID：");
            updateID = scanner.next();

            boolean exists = studentService.isExists(updateID);
            if (!exists) {
                System.out.println("查不到该ID，请重新输入");
            }else{
                break;
            }
        }
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        System.out.println("请输入学生年龄");
        String age = scanner.next();
        System.out.println("请输入学生生日");
        String birthday = scanner.next();

        Student newStudent=new Student();
        newStudent.setId(updateID);
        newStudent.setName(name);
        newStudent.setAge(age);
        newStudent.setBirthday(birthday);

        studentService.updateStudent(updateID,newStudent);
        System.out.println("修改成功");

    }

    private void deleteStudentById() {
            StudentService studentService=new StudentService();
            String deleteID;
        while (true){
            System.out.println("请输入要删除的学生ID：");
            Scanner scanner=new Scanner(System.in);
            deleteID = scanner.next();

            boolean exists = studentService.isExists(deleteID);
            if (!exists) {
                System.out.println("查不到该ID，请重新输入");
            }else{
                break;
            }
        }
            studentService.deleteStudentById(deleteID);
        System.out.println("删除成功");

    }

    private void findAllStudent() {
        StudentService studentService=new StudentService();
        Student[] students=studentService.findAllStudent();
        if (students==null) {
            System.out.println("没有任何学生信息，请添加后再查询");
            return;
        }
        System.out.println("学生\t\t姓名\t年龄\t生日");
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (null!=student) {
                System.out.println(student.getId()+"\t"+student.getName()+"\t"
                        +student.getAge()+"\t\t"+student.getBirthday());
            }
        }
    }

    private void addStudent() {

        Scanner scanner = new Scanner(System.in);
        StudentService studentService = new StudentService();
        String id;
        while (true) {
            System.out.println("请输入学生学号：");
            id = scanner.next();

            boolean flag = studentService.isExists(id);
            if (flag)
                System.out.println("学号已被占用，请重新输入！");
            else break;
        }
        System.out.println("请输入学生姓名：");
        String name = scanner.next();
        System.out.println("请输入学生年龄：");
        String age = scanner.next();
        System.out.println("请输入学生生日：");
        String birthday = scanner.next();

        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        student.setBirthday(birthday);

        boolean result = studentService.addStudent(student);

        if (result)
            System.out.println("添加成功！");
        else
            System.out.println("添加失败！");
    }
}
